/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Forms;

import BusinessEntity.BE_Personal;
import BusinessEntity.BE_Requerimiento;
import BusinessEntity.BE_Usuario;
import BusinessLogic.BL_Personal;
import BusinessLogic.BL_Requerimiento;
import Comun.Constantes;
import Comun.Funciones_Comunes;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JInternalFrame;
import javax.swing.ListSelectionModel;

/**
 *
 * @author richard.saldana
 */
public class Frm_AsignarRequerimiento extends javax.swing.JInternalFrame {

    private JInternalFrame frmPadre;
    //Personal
    private BE_Personal objBEPersonal;
    //usuario
    private BE_Usuario objBEUsuario;
    //combo Analista
    private ArrayList<BE_Personal> aobjBE_comboPersonal;
    //Requerimiento Seleccionado
    private BE_Requerimiento objBERequerimientoSeleccionado;
    //Opcion
    private int intOpcion;
    /**
     * Creates new form Frm_AsignarRequerimiento
     */
    public Frm_AsignarRequerimiento(JInternalFrame frmVentana, int intCodigoRequerimiento, int intOpcion) throws InstantiationException, IllegalAccessException, SQLException {
        initComponents();
        setDefaultCloseOperation(0);
        this.frmPadre = frmVentana;
        this.objBEPersonal = ((Frm_BandejaTarea) this.frmPadre).getObjBEPersonal();
        this.objBEUsuario = ((Frm_BandejaTarea) this.frmPadre).getObjBEUsuario();
        this.intOpcion = intOpcion;
        inicializar(intCodigoRequerimiento);
    }

    private void inicializar(int intCodigoRequerimiento) throws InstantiationException, IllegalAccessException, SQLException{
        
        this.objBERequerimientoSeleccionado = BL_Requerimiento.obtenerInstancia().obtenerRequerimientoID(intCodigoRequerimiento);
        txtNumRequerimiento.setText(Integer.toString(getObjBERequerimientoSeleccionado().getIdRequerimiento()));
        txtNombre.setText(getObjBERequerimientoSeleccionado().getStrNombre());
        jtxDescripcion.setText(getObjBERequerimientoSeleccionado().getStrDescripcion());
        //Datos de Combo segun la opcion
        setAobjBE_comboPersonal(BL_Personal.obtenerInstancia().obtenerPersonalAsignacion());
        if(this.aobjBE_comboPersonal != null){
            cmbPersonalAsig.removeAllItems();
            this.cmbPersonalAsig.addItem("<Seleccionar>");
            for (int i = 0; i < this.aobjBE_comboPersonal.size(); i++) {
                this.cmbPersonalAsig.addItem(this.aobjBE_comboPersonal.get(i));
                
            }
        }
        if(this.intOpcion == Constantes.OpcionModificar){
        BE_Personal objBE_Personal = new BE_Personal();
        objBE_Personal.setIdPersonal(this.objBERequerimientoSeleccionado.getIntUsuarioAsignado());
        this.cmbPersonalAsig.setSelectedItem(objBE_Personal);
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpRequerimientos = new javax.swing.JPanel();
        lblNombre = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        lblDescripcion = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtxDescripcion = new javax.swing.JTextArea();
        lblPrioridad = new javax.swing.JLabel();
        cmbPersonalAsig = new javax.swing.JComboBox();
        lblResponsable = new javax.swing.JLabel();
        txtNumRequerimiento = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        jpRequerimientos.setBorder(javax.swing.BorderFactory.createTitledBorder("Asignar Requerimiento"));

        lblNombre.setText("Nombre:");

        txtNombre.setEnabled(false);

        lblDescripcion.setText("Descripción:");

        jtxDescripcion.setColumns(20);
        jtxDescripcion.setRows(5);
        jtxDescripcion.setEnabled(false);
        jScrollPane1.setViewportView(jtxDescripcion);

        lblPrioridad.setText("Analista Asignación:");

        cmbPersonalAsig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbPersonalAsigActionPerformed(evt);
            }
        });

        lblResponsable.setText("Requerimiento Nº:");

        txtNumRequerimiento.setEnabled(false);

        javax.swing.GroupLayout jpRequerimientosLayout = new javax.swing.GroupLayout(jpRequerimientos);
        jpRequerimientos.setLayout(jpRequerimientosLayout);
        jpRequerimientosLayout.setHorizontalGroup(
            jpRequerimientosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpRequerimientosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpRequerimientosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpRequerimientosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jpRequerimientosLayout.createSequentialGroup()
                            .addComponent(lblResponsable)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtNumRequerimiento, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(lblNombre)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jpRequerimientosLayout.createSequentialGroup()
                            .addComponent(lblDescripcion)
                            .addGap(34, 34, 34)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jpRequerimientosLayout.createSequentialGroup()
                        .addComponent(lblPrioridad)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmbPersonalAsig, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpRequerimientosLayout.setVerticalGroup(
            jpRequerimientosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpRequerimientosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpRequerimientosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblResponsable)
                    .addComponent(txtNumRequerimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNombre)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpRequerimientosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDescripcion)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jpRequerimientosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPrioridad)
                    .addComponent(cmbPersonalAsig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/guardar32.png"))); // NOI18N
        btnAceptar.setToolTipText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/eliminar32.png"))); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnAceptar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancelar)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpRequerimientos, javax.swing.GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jpRequerimientos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmbPersonalAsigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbPersonalAsigActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbPersonalAsigActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed

        
        boolean bolComboPersonalAsignado = Funciones_Comunes.validarCombo(cmbPersonalAsig.getSelectedItem().toString(), "<Seleccionar>", lblPrioridad.getText());

        if(bolComboPersonalAsignado){
            BE_Personal objBE_Personal = (BE_Personal)this.cmbPersonalAsig.getSelectedItem();

            
            BE_Requerimiento objRequerimiento = BL_Requerimiento.obtenerInstancia().crearObjetoRequerimiento(this.objBERequerimientoSeleccionado.getStrNombre(),this.objBERequerimientoSeleccionado.getStrDescripcion(), null, null, objBE_Personal.getObjBEUsuario().getIdUsuario(), this.objBERequerimientoSeleccionado.getIdRequerimiento());


            try {
                if (Comun.GUI.msjPreguntaSiNo("¿Seguro que desea Asignar el Requerimiento Nº "+this.objBERequerimientoSeleccionado.getIdRequerimiento() )) {
                    BL_Requerimiento.obtenerInstancia().asignarRequerimiento(objRequerimiento);
                    Comun.GUI.msjAlerta("Se Asignó el Requerimiento Correctamente");
                }

            } catch (InstantiationException ex) {
                Logger.getLogger(Frm_AsignarRequerimiento.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Frm_AsignarRequerimiento.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(Frm_AsignarRequerimiento.class.getName()).log(Level.SEVERE, null, ex);
            }

            ((Frm_BandejaTarea) (this.frmPadre)).setVisible(true);
            try {            
                ((Frm_BandejaTarea) (this.frmPadre)).cargarListaRequerimientos(this.objBEPersonal);
            } catch (InstantiationException ex) {
                Logger.getLogger(Frm_AsignarRequerimiento.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Frm_AsignarRequerimiento.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(Frm_AsignarRequerimiento.class.getName()).log(Level.SEVERE, null, ex);
            }
     
            this.dispose();
        }

    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        ((Frm_BandejaTarea) (this.frmPadre)).setVisible(true);
        ((Frm_BandejaTarea) (this.frmPadre)).tblRequerimientos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        try {
            ((Frm_BandejaTarea) (this.frmPadre)).cargarListaRequerimientos(this.objBEPersonal);
        } catch (InstantiationException ex) {
            Logger.getLogger(Frm_Requerimiento.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Frm_Requerimiento.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Frm_Requerimiento.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.dispose();
        // TODO add your handling code here:
    }//GEN-LAST:event_btnCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JComboBox cmbPersonalAsig;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel jpRequerimientos;
    private javax.swing.JTextArea jtxDescripcion;
    private javax.swing.JLabel lblDescripcion;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblPrioridad;
    private javax.swing.JLabel lblResponsable;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtNumRequerimiento;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the objBEPersonal
     */
    public BE_Personal getObjBEPersonal() {
        return objBEPersonal;
    }

    /**
     * @return the objBEUsuario
     */
    public BE_Usuario getObjBEUsuario() {
        return objBEUsuario;
    }

    /**
     * @return the objBERequerimientoSeleccionado
     */
    public BE_Requerimiento getObjBERequerimientoSeleccionado() {
        return objBERequerimientoSeleccionado;
    }

    /**
     * @param objBERequerimientoSeleccionado the objBERequerimientoSeleccionado to set
     */
    public void setObjBERequerimientoSeleccionado(BE_Requerimiento objBERequerimientoSeleccionado) {
        this.objBERequerimientoSeleccionado = objBERequerimientoSeleccionado;
    }

    /**
     * @return the aobjBE_comboPersonal
     */
    public ArrayList<BE_Personal> getAobjBE_comboPersonal() {
        return aobjBE_comboPersonal;
    }

    /**
     * @param aobjBE_comboPersonal the aobjBE_comboPersonal to set
     */
    public void setAobjBE_comboPersonal(ArrayList<BE_Personal> aobjBE_comboPersonal) {
        this.aobjBE_comboPersonal = aobjBE_comboPersonal;
    }

    /**
     * @return the intOpcion
     */
    public int getIntOpcion() {
        return intOpcion;
    }

    /**
     * @param intOpcion the intOpcion to set
     */
    public void setIntOpcion(int intOpcion) {
        this.intOpcion = intOpcion;
    }


}
